<template>
  <div class="home-page">
    <div class="banner">
      <div class="container">
        <h1 class="logo-font">conduit</h1>
        <p>A place to share your knowledge.</p>
      </div>
    </div>

    <div class="container page">
      <div class="row">
        <div class="col-md-9">
          <div class="feed-toggle">
            <ul class="nav nav-pills outline-active">
              <li v-if="user" class="nav-item">
                <nuxt-link
                  class="nav-link"
                  :class="tab === 'your_feed' ? 'active' : ''"
                  :to="{
                    name: 'home',
                    query: {
                      tab: 'your_feed',
                    },
                  }"
                  exact
                >
                  Your Feed
                </nuxt-link>
              </li>
              <li class="nav-item">
                <nuxt-link
                  class="nav-link"
                  :class="tab === 'global_feed' ? 'active' : ''"
                  :to="{
                    name: 'home',
                  }"
                  exact
                >
                  Global Feed
                </nuxt-link>
              </li>
              <li v-if="tag" class="nav-item">
                <nuxt-link
                  class="nav-link"
                  :class="tab === 'tag_feed' ? 'active' : ''"
                  :to="{
                    name: 'home',
                    query: {
                      tab: 'tag_feed',
                      tag,
                    },
                  }"
                  exact
                >
                  <i class="ion-pound"></i>
                  {{ tag }}/{{ tab === 'tag_feed' }}
                </nuxt-link>
              </li>
            </ul>
          </div>

          <Arttie v-for="art in articles" :key="art.slug" :art="art"></Arttie>

          <!-- 分页 -->
          <Paggie
            :total="totalPage"
            :page="page"
            :to="{
              name: 'home',
              query: { tag, tab },
            }"
          ></Paggie>
          <!-- /分页 -->
        </div>

        <div class="col-md-3">
          <div class="sidebar">
            <p>Popular Tags</p>

            <div class="tag-list">
              <nuxt-link
                v-for="tag in tagList"
                :key="tag"
                class="tag-pill tag-default"
                :to="{
                  name: 'home',
                  query: { tag, tab: 'tag_feed' },
                }"
              >
                {{ tag }}
              </nuxt-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getTag } from '@/api/tag'
import { listArticle, feedArticle } from '@/api/article'
import { mapState } from 'vuex'
import Arttie from '@/components/arttie'
import Paggie from '@/components/paggie'

export default {
  name: 'Index',
  components: { Arttie, Paggie },
  async asyncData({ query }) {
    const limit = 10
    const page = Number.parseInt(query.page || 1)
    const tab = query.tab || 'global_feed'
    const tag = query.tag
    const loadList = tab === 'your_feed' ? feedArticle : listArticle
    const [{ articles, articlesCount }, { tags }] = await Promise.all([
      loadList({
        limit,
        offset: (page - 1) * limit,
        tag,
      }),
      getTag(),
    ])
    return {
      tag,
      tab,
      page,
      limit,
      articles,
      articlesCount,
      tagList: tags.length > 20 ? tags.slice(0, 20) : tags,
    }
  },
  watchQuery: ['page', 'tag', 'tab'],
  computed: {
    ...mapState(['user']),
    totalPage() {
      const pages = Math.ceil(Number.parseInt(this.articlesCount) / this.limit)
      return pages >= 50 ? 50 : pages
    },
  },
  methods: {},
}
</script>

<style></style>
